class Node():
    def __init__(self,data,pointer):
        self.data = data
        self.pointer = pointer

def pushNode(item,startPointer,freePointer):
    if freePointer == -1:
        return startPointer,freePointer
    else:
        newNodePos = freePointer
        stack[newNodePos].data = item
        freePointer = stack[freePointer].pointer
        stack[newNodePos].pointer = -1
        if startPointer == -1:
            startPointer = newNodePos
            return startPointer,freePointer
        else:
            thisNode = startPointer
            while thisNode != -1:
                preNode = thisNode
                thisNode = stack[thisNode].pointer
            stack[preNode].pointer = newNodePos
            return startPointer,freePointer

def popNode(startPointer,freePointer):
    if freePointer == 0:
        return startPointer,freePointer
    else:
        if stack[startPointer].pointer == -1:
            thisNode = startPointer
            startPointer = -1
        else:
            preNode = -1
            thisNode = startPointer
            while stack[thisNode].pointer != -1:
                preNode = thisNode
                thisNode = stack[thisNode].pointer
            stack[preNode].pointer = -1
        stack[thisNode].pointer = freePointer
        freePointer = thisNode
        return startPointer,freePointer

def printStack(stack,thispointer):
    while thispointer!=-1:
        print(stack[thispointer].data)
        thispointer = stack[thispointer].pointer

if __name__ == '__main__':
    stack = [Node(None,1),Node(None,2),Node(None,-1)]
    startPointer = -1
    freePointer = 0
    startPointer,freePointer =  pushNode(1,startPointer,freePointer)
    startPointer,freePointer =  pushNode(2,startPointer,freePointer)
    startPointer,freePointer =  pushNode(3,startPointer,freePointer)
    printStack(stack,startPointer)
    startPointer, freePointer = popNode(startPointer, freePointer)
    startPointer, freePointr = popNode(startPointer, freePointer)
    printStack(stack,startPointer)
